/**
 * Created by fanyongsheng on 2017/7/12.
 */
var base64 = require('./base64.ts');
var private_fuc = function(getway){
    var fuc  ={};
    fuc.createStream = function(obj,deviceObj){
        if(obj){
            getway.createStream(obj.id,deviceObj);
        }else{
            getway.createStream('',deviceObj);
        }

    };



    /**
     * 重连方法
     */
    fuc.tokenconnect = function(addr){
        setTimeout(function(){
            getway.connect(addr);
        },6000);
    };


    //设置流
    fuc.strema_video = function(info,dwStreamIndex,infoname,value) {
        var obj = {}, streamindex_video = info.streamindex_video, k;
        switch (infoname) {
            case BRAC_SO_LOCALVIDEO_DEVICENAME:
                //设置流号
                obj.dwStreamIndex = dwStreamIndex;
                obj.value = value;
                streamindex_video.push(obj);
                break;
            case BRAC_SO_LOCALVIDEO_WIDTHCTRL:
                //分辨率宽度
                k = getStream(dwStreamIndex);
                if (k == -1) {

                } else {
                    streamindex_video[k].width = value;
                }
                break;
            case BRAC_SO_LOCALVIDEO_HEIGHTCTRL:
                //分辨率高度
                k = getStream(dwStreamIndex);
                if (k == -1) {

                } else {
                    streamindex_video[k].height = value;
                }
                break;
            case BRAC_SO_LOCALVIDEO_FPSCTRL:
                //帧率
                k = getStream(dwStreamIndex);
                if (k == -1) {

                } else {
                    streamindex_video[k].fpsctrl = value;
                }
                break;
            case BRAC_SO_LOCALVIDEO_BITRATECTRL:
                //码率
                k = getStream(dwStreamIndex);
                if (k == -1) {

                } else {
                    streamindex_video[k].fpsctrl = value;
                }
                break;
        }

        function getStream(dwStreamIndex){
            var arr =  info.streamindex_video;
            var k = -1;
            for(var i in arr){
                if(arr[i].dwStreamIndex == dwStreamIndex){
                    k = i;
                }
            }
            return k;
        }

        return streamindex_video;
    };

    // 获取当前浏览器
    fuc.getBrowser = function (){
        var browser = "unknown browser";
        var ua = navigator.userAgent.toLowerCase();

        var info = {
            ie: /msie/.test(ua) && !/opera/.test(ua),
            op: !/msie/.test(ua) && /opera/.test(ua),
            sa: /version.*safari/.test(ua),
            ch: /chrome/.test(ua) && window.navigator.webkitPersistentStorage,
            ff: /firefox/.test(ua),
            qh360: /chrome/.test(ua) && !window.navigator.webkitPersistentStorage,
            qq: /qqbrowser/.test(ua),
            sg: /metasr/.test(ua)
        };

        if (info.ch){
            browser = "Chrome";
        }else if (info.ie){
            browser = "IE";
        }else if (info.ff){
            browser = "Firefox";
        }else if (info.sa){
            browser = "Safari";
        }else if (info.qh360){
            browser = "360浏览器";
        }else if (info.op){
            browser = "Opera";
        }else if (info.qq){
            browser = "QQ浏览器";
        }else if (info.sg){
            browser = "搜狗浏览器";
        }

        return browser;
    };


    fuc.log = function(message){

        console.log(message);
    };

    /*
    关闭所有
     */
    fuc.closeAll = function(videoStream){
        getway.closeAllPeerConnection();
        getway.removeVideo();

        try{
            log('关闭摄像头');
            videoStream[0].getVideoTracks()[0].stop();
        }catch(e){
            log('摄像头关闭失败');
            log(e);
        }
    };

    /*
    关闭自己
     */
    fuc.closeMyCamera = function(videoStream){
        try{
            log('关闭自己摄像头');
            videoStream[0].getVideoTracks()[0].stop();
            getway.removeMyVideo();
        }catch(e){
            log('摄像头关闭失败');
            log(e);
        }
    };

    fuc.closeOtherCamera = function(pid){
        getway.closeOtherPeerConnection(pid);
        getway.removeOtherVideo(pid);
    };


    //链接成功后发送setsdkoption
    fuc.SetSDKOptionString = function(SetSDKOptionStringArr){
        for(var i in SetSDKOptionStringArr){
            var data = {};
            data = SetSDKOptionStringArr[i];
            getway.socket_send('request',0,'setsdkoptionstring',data);
        }
    };

    //链接成功后发送setsdkoption
    fuc.SetSDKOptionInt = function(SetSDKOptionIntArr){
        for(var i in SetSDKOptionIntArr) {
            var data = {};
            data = SetSDKOptionIntArr[i];
            getway.socket_send('request',0,'setsdkoptionint',data);
        }

    };


    fuc.base64encode = base64.base64encode;
    fuc.base64decode = base64.base64decode;


    return fuc;
};



exports.instance = private_fuc;


